package com.burpee.web;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.apache.catalina.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.burpee.dao.FoodDao;
import com.burpee.dao.FoodDaoImpl;
import com.burpee.service.ChattingService;
import com.burpee.service.ExerciseService;
import com.burpee.service.FoodService;
import com.burpee.service.GroupsService;
import com.burpee.ui.Chatting;
import com.burpee.ui.Food;
import com.burpee.ui.Groups;
import com.burpee.ui.Statistics;
import com.burpee.xml.ChattingXml;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.thoughtworks.xstream.XStream;

@Controller
@RequestMapping("/test.spring")
public class TestController {
	@Autowired
	GroupsService groupsService;
	@Autowired
	ChattingService chattingService;
	@Autowired ExerciseService exerciseService;
	@Autowired FoodService foodService;
//	@Autowired
//	CommentsService commentsService;

	@RequestMapping(params="action=chattingxml")
	public ModelAndView chattingadd(HttpServletRequest request, HttpServletResponse response) {
				
		ChattingXml chattingXml = new ChattingXml(chattingService.list());
		
		XStream xStream = new XStream();
		
		xStream.alias("chattingXmls", ChattingXml.class);
		xStream.alias("chattingXml", Chatting.class);
		xStream.addImplicitCollection(ChattingXml.class, "chattingList");
		
		String xml = xStream.toXML(chattingXml);
		
		System.out.println(xml);
		
		return null;

	}
	
	
//	@RequestMapping(params="action=boarditemsadd")
//	public ModelAndView boarditemsadd(HttpServletRequest request, HttpServletResponse response) {
//				int i;
//				String a;
//				String b="연우신";
//				for(i=0;i<10;i++)
//				{
//					Boarditems boarditems = new Boarditems();
//					a = b+"님의" + i + "번째 글입니다!!.";
//					
//					boarditems.setBoarditemsTitle(a);
//					boarditems.setBoarditemsView(b+" 맘대로 마구마구 입력할테다");
//					boarditems.setInputDate(new Date(System.currentTimeMillis()));
//					boarditems.setUpdateDate(new Date(System.currentTimeMillis()));
//					boarditems.setBoarditemsViewCnt(0);
//					boarditems.setBoarditemsNoticeYn("N");
//					boarditems.setBoarditemsRemoveYn("N");
//					boarditems.setMemberId("jungyw");
//					boarditems.setBoardNo(1);
//
//					boardItemsService.add(boarditems);
//					
//					
//		
//				}
//
//
//		return null;
//
//
//	}
	
//	@RequestMapping(params="action=commentsadd")
//	public ModelAndView commentsadd(HttpServletRequest request, HttpServletResponse response) {
//				int i;
//				String a;
//				for(i=1;i<=5;i++)
//				{
//					Comments comments = new Comments();
//					a = "버피님의" + i + "번째 글입니다!!.";
//					
//					comments.setCommentView("부모키없어요");
//					comments.setInputDate(new Date(System.currentTimeMillis()));
//					comments.setMemberId("burpee");
//					comments.setBoarditemsNo(3);
//					comments.setCommentLineNo(2);
//					
//					commentsService.add(comments);		
//				}
//
//
//		return null;
//
//
//	}
	
//	@RequestMapping(params="action=groupadd")
//	public ModelAndView groupadd(HttpServletRequest request, HttpServletResponse response) {
//				int i;
//				String a;
//				String b;
//				for(i=1;i<=100;i++)
//				{
//					Groups groups = new Groups();
//					a = "버피님의" + i + "번째 글입니다!!.";
//					b = i + "번방";
//					groups.setGroupName(b);
//					groups.setGroupView(a);
//					
//					groupsService.add(groups);		
//				}
//
//
//		return null;
//
//
//	}
	
	@RequestMapping(method=RequestMethod.POST,params ="action=test")
	public ModelAndView statistics(HttpServletRequest request,
								   HttpServletResponse response,
								   Statistics statistics) {
	   ObjectMapper obj = new ObjectMapper();

	   Map<String, String>map = new HashMap<String, String>();
	   List<Map<String,String>>list = new ArrayList<Map<String, String>>();
	   List<Food>foodList = null;
	   map.put("name", "ss");
	   map.put("key", "value");
	   list.add(map);
	   

	   foodList = foodService.list();
	   ModelAndView modelAndView = new ModelAndView();
	   	
       modelAndView.setViewName("healthWritteBook/healthCareMain1");
	   modelAndView.addObject("food", foodList);
			try {
				String json = obj.writeValueAsString(list);
				response.setContentType("application/json; charset=UTF-8");
				response.getWriter().print(json);
			} catch (JsonGenerationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JsonMappingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return modelAndView;
	}
	@RequestMapping(method=RequestMethod.POST,params ="action=json")
	public ModelAndView json(HttpServletRequest request,
								   HttpServletResponse response,
								   Statistics statistics) {
	   ObjectMapper obj = new ObjectMapper();

	   Map<String, String>map = new HashMap<String, String>();
	   List<Map<String,String>>list = new ArrayList<Map<String, String>>();
	   List<Food>foodList = null;
	   map.put("name", "ss");
	   map.put("key", "value");
	   list.add(map);
	   
	   StringBuffer sb = new StringBuffer();
	   sb.append("fewj;fwej;j");

	   foodList = foodService.list();
	   ModelAndView modelAndView = new ModelAndView();
	   modelAndView.addObject("statis", sb);
	   modelAndView.setViewName("healthWritteBook/healthCareMain1");
			try {
				String json = obj.writeValueAsString(foodList);
				response.setContentType("application/json; charset=UTF-8");
				response.getWriter().print(json);
			} catch (JsonGenerationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JsonMappingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return modelAndView;
	}
}
	


